package com.tanhua.server.service;

import com.tanhua.commons.templates.HuanXinTemplate;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.domain.vo.UserInfoVo;
import com.tanhua.dubbo.api.FriendApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.server.Interceptor.UserHolder;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

@Service
public class IMService {

    @Reference
    private UserInfoApi userInfoApi;

    @Reference
    private FriendApi friendApi;

    @Autowired
    private HuanXinTemplate huanXinTemplate;


    public ResponseEntity<Object> getHuanxinUserInfo(Long huanxinId) {
        //1. 查询用户的信息
        UserInfo userInfo = userInfoApi.findById(huanxinId);
        //2
        UserInfoVo userInfoVo = new UserInfoVo();
        //3. 属性拷贝
        BeanUtils.copyProperties(userInfo,userInfoVo);
        if(userInfo.getAge()!=null){
            userInfoVo.setAge(userInfo.getAge()+"");
        }

        return ResponseEntity.ok(userInfoVo);

    }

    //添加陌生人为好友
    public ResponseEntity<Object> addContact(Integer friendUserId) {
        //1. 获取当前登陆者
        long userId = UserHolder.getUserId();
        //2. 添加为好友
        friendApi.save(userId, friendUserId);
        //3. 环信要记录他们是好友
        huanXinTemplate.contactUsers(userId,friendUserId.longValue());
        return ResponseEntity.ok(null);
    }


    /**
     * 查询好友列表
     * @param page
     * @param pagesize
     * @param keyword
     * @return
     */
    public ResponseEntity<Object> queryContractList(Integer page, Integer pagesize, String keyword) {
        PageResult pageResult = friendApi.queryContractList(UserHolder.getUserId(),page,pagesize,keyword);
        return ResponseEntity.ok(pageResult);
    }
}
